package twoD.hofem;

/**
 * @author Team Malamat
 */
public class HierarchicFB extends AbstractPolynomialBasisOnR {

	/**
	 * Constructs a Hierarchic basis of degree 2.
	 */
	public HierarchicFB() {
		setP(2);
	}

	/**
	 * Constructs a Hierarchic basis of the specified degree.
	 * 
	 * @param p
	 *            polynomial degree
	 */
	public HierarchicFB(int p) {
		setP(p);
	}

	/**
	 * The Hierarchic polynomial function: N0(x) = (1-x)/2 N1(x) = (1+x)/2 Nj(x)
	 * = (Pj(x) - P(j-2)(x))/sqrt(2*(2*j-1)) <j = 2,3...> where Pj(x) is
	 * Legendre polynomial
	 */
	public FunctionRToR[] createBasis(int p) {
		FunctionRToR[] shapeFunctions = new FunctionRToR[p + 1];

		LegendreFB basis = new LegendreFB();
		FunctionRToR[] fLeg = basis.createBasis(p + 1);

		if (p >= 0) {
			shapeFunctions[0] = new PolynomialRToR(-1, 1, 0.5, -0.5);
		}
		if (p >= 1) {
			shapeFunctions[1] = new PolynomialRToR(-1, 1, 0.5, 0.5);
		}

		if (p >= 2) {
			for (int i = 2; i <= p; i++) {
				PolynomialRToR p1 = ((PolynomialRToR) fLeg[i - 2])
						.multiply(-1.0);
				PolynomialRToR p2 = (((PolynomialRToR) fLeg[i]).add(p1));
				shapeFunctions[i] = p2.multiply(1.0 / Math
						.sqrt(2 * (2 * i - 1)));
			}

		}
		return shapeFunctions;
	}

	public Interval getDomain() {
		return new Interval(-1, 1);
	}

	@Override
	public String getName() {
		return "Hierarchic";
	}
}
